//
//  FeedBackViewController.swift
//  LongTermInsurance
//
//  Created by DYD on 2018/11/21.
//  Copyright © 2018 HeChao. All rights reserved.
//

import UIKit
import RxCocoa
import RxSwift

class FeedBackViewController: BaseViewController {

    
    @IBOutlet weak var textView: UITextView!
    
    @IBOutlet weak var placeholderLabel: UILabel!
    @IBOutlet weak var submitButton: UIButton!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        title = "意见反馈"
        
        _ = textView.rx.text.subscribe(onNext: {[weak self] (text) in
            guard let str = text else {
                self?.placeholderLabel.isHidden = false
                return
            }
            self?.placeholderLabel.isHidden = str.count > 0
            if str.count > 800 {
                self?.textView.text = String(str.prefix(800))
            }
        }, onError: nil, onCompleted: nil, onDisposed: nil)
    }

    
    @IBAction func submintButtonAction(_ sender: UIButton) {
        
        guard let text = textView.text, text.count > 0 else {
            makeToast("请先输入内容~")
            return
        }
        let json = ["APPLB": "1",
                    "Message": text].convertToJsonString()
        let paramString = "<json>"+json+"</json>"
        
        NetworkingHandle.fetchNetworkData(method: "saveOpinionMessage", paramString: paramString, success: {[weak self] (dict) in
            if let code = dict["ResultCode"] as? Int,code == 0 {
                CDUtility.showMSG(message: "提交成功！")
                self?.navigationController?.popViewController(animated: true)
                return
            }
            if let code = dict["ResultCode"] as? Int,code == 1 {
                if let msgString = dict["ResultMessage"] as? String {
                    CDUtility.showMSG(message: msgString)
                }
            }
        }) {
            print("")
        }
        
    }
    
    

}
